/**
 * 广告支出-按广告名称统计(按条展示)
 */   
function GetRequest() {
    var url = decodeURI(location.search);
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

//页面加载事件
$(function(){
	var object = GetRequest();
	var page;
	init(1,null);
	function init(page,adNumber){
		var initTable="<tr>" +
				"<th>序号</th>" +
				"<th>时间</th>" +
				"<th>广告名称</th>" +
				"<th>广告编号</th>" +
				"<th>行业属性</th>" +
				"<th>广告形式</th>" +
				"<th>投放终端</th>" +
				"<th>平均单价</th>" +
				"<th>点击次数</th>" +
				"<th>广告支出</th>" +
				"<th>支出/总支出</th>" +
				"<th>总广告预算</th>" +
				"<th>支出排名</th>" +
				"</tr>";

		$('#AdName').html(initTable);
		
		if(page==null || page.length<=0){
			page=1;
		}
		
		
		var endTime = getQueryString("endTime"),startTime=getQueryString("startTime");
		var adName=object.adName;
		var adNumber=getQueryString("adNumber");
		$("#endTime").val(endTime);
		$("#startTime").val(startTime);
		$("#adName").val(object.adName);
		if(endTime&&startTime){
	    	$.ajax({
	    		url : '/Lvscore_Advertiser_Maven/byAdname',
	            data:{"startTime":startTime,"endTime":endTime,"adName":adName,"page":page,"adNumber":adNumber},
	            dataType:'json',
	    		type : 'post',
	    		success : function(data) {
	    			var MyPays=0;	
					var beans = data.beans;
					var proportions=0;
					var recharges=0;
					var adBudgets=0;
					var clickNums=0;
	    			$('#pageBox').extendPagination({
	    				totalCount:data.total,
	    		 		showPage: data.pages+1,
	    	            limit: data.pageSize,
	    	            goPage:page,
	    	            callback: function (page) {
	    	            	ByAdNameTwo(page);
	    	            }
	    			});
	    			
	    			
	    			//循环输出表格
	    			for(var i = 0; i < beans.length;i++){
	    				if(beans[i].device=="pc"){
							beans[i].device="PC";
						}else if(beans[i].device=="mobile"){
							beans[i].device="手机";
						}
	    				if(beans[i].adForm=="video"){
							beans[i].adForm="视频";
						}else if(beans[i].adForm=="picture"){
							beans[i].adForm="图片";
						}
	    				
	    				var tbody = "<tr><td>"+(i+1)+"</td>" +
	    				"<td>"+beans[i].time +"</td>" +
	    				"<td>"+beans[i].adName +"</td>" +
	    				"<td>"+beans[i].adNum+"</td>" +
	    				"<td>"+beans[i].industry+"</td>" +
	    				"<td>"+beans[i].adForm+"</td>" +
	    				"<td>"+beans[i].device+"</td>" +
	    				"<td>"+(beans[i].averagePrice*1).toFixed(2)+"</td>" +
						"<td>"+beans[i].clickNum+"</td>" +
						"<td>"+beans[i].adPay+"</td>" +
	    				"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
	    				"<td>"+beans[i].adBudget+"</td>" +
	    				"<td>"+((page-1)*5+(i+1))+"</td>"+
	    				"</tr>"
	    				
	    				$('#AdName').append(tbody);
	    			}
	    			var allBean=data.allBean;
					for (var i = 0; i < allBean.length;i++) {
						MyPays+=allBean[i].adPay*1;
						var proportion=(allBean[i].proportion*100).toFixed(2);
						proportions+=proportion*1;
						adBudgets+=allBean[i].adBudget*1;
						clickNums+=allBean[i].clickNum*1;
					}
	    			var tbodyTwo="<tr>" +
	    			"<td>总计</td>" +
	    			"<td>---</td>" +
	    			"<td>---</td>" +
	    			"<td>---</td>" +
	    			"<td>---</td>" +
	    			"<td>---</td>" +
	    			"<td>---</td>" +
	    			"<td>---</td>" +
	    			"<td>"+clickNums+"</td>" +
					"<td>"+MyPays.toFixed(2)+"</td>" +
				    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
				    "<td>"+adBudgets.toFixed(2)+"</td>" +
	    		    "<td>---</td>" + 
	    		 "</tr>";
	    			if(data.pages==data.pageNum){
	    				$('#AdName').append(tbodyTwo);
					}
	    		},
	            error: function(){}        
	    	});
		}
	}
	//跳转输入值正则验证
	function inputpattern(){
		var inputVal=$("input[name='selectPage']").val();
		var patInput=/^[1-9]\d*$/;   //正整数
		if(patInput.test(inputVal)){
			return true;
		}else{
			return false;
		}
	}
	$(".pageBox button").css("background-color","#478df1");
	$(".pageBox button").on("click",function(){
		var result=inputpattern();
		var showPageN=$(".pagination").children().length;
		var activeLi=$(".active a").html();
		page=$("input[name='selectPage']").val();
		if(result){
			if(page==activeLi){
				
			}else{
				if(page==1){
					var adName = $("#adName").val();
					init(page,adName);
				}else if(page>showPageN-4){
					
				}else{
					total(page);
				}
			}
		}
		$("input[name='selectPage']").val("");
	});
	
	
})
//跳转到第几页
function total(page){
	var startTime=GetRequest().startTime;
	var endTime=GetRequest().endTime;
	var adName=GetRequest().adName;
	if(endTime&&startTime){
    	$.ajax({
    		url : '/Lvscore_Advertiser_Maven/byAdname',
            data:{"startTime":startTime,"endTime":endTime,"adName":adName,"page":page},
            dataType:'json',
    		type : 'post',
    		success : function(data) {
    			$('#pageBox').extendPagination({
    				totalCount:data.total,
    		 		showPage: data.pages+1,
    	            limit: data.pageSize,
    	            goPage:page,
    	            callback: function (page) {
    	            	ByAdNameTwo(page);
    	            }
    			});
    		},
            error: function(){}        
    	});
	}
}

//查询出发的事件
function ByAdName(startTime,endTime,adName,page){
	if(page==null || page.length<=0){
		var page=1;
	}
	//var adNumber=getQueryString("adNumber");
	var adName=$("#adName").val();
	$.ajax({
		url : '/Lvscore_Advertiser_Maven/byAdname',
        data:{"startTime":startTime,"endTime":endTime,"page":page,"adName":adName},
        dataType:'json',
		type : 'post',
		success : function(data) {
			$('#AdName').find("tr").eq(0).siblings("tr").remove();
			var MyPays=0;	
			var beans = data.beans;
			var proportions=0;
			var recharges=0;
			var adBudgets=0;
			var clickNums=0;
			
			$('#pageBox').extendPagination({
				totalCount:data.total,
		 		showPage: data.pages+1,
	            limit: data.pageSize,
	            goPage:page,
	            callback: function (page) {
	            	ByAdNameTwo(page);	
	            }
			});
			//循环输出表格
			for(var i = 0; i < beans.length;i++){
				if(beans[i].device=="pc"){
					beans[i].device="PC";
				}else if(beans[i].device=="mobile"){
					beans[i].device="手机";
				}
				if(beans[i].adForm=="video"){
					beans[i].adForm="视频";
				}else if(beans[i].adForm=="picture"){
					beans[i].adForm="图片";
				}
				var tbody = "<tr><td>"+(i+1)+"</td>" +
				"<td>"+beans[i].time +"</td>" +
				"<td>"+beans[i].adName +"</td>" +
				"<td>"+beans[i].adNum+"</td>" +
				"<td>"+beans[i].industry+"</td>" +
				"<td>"+beans[i].adForm+"</td>" +
				"<td>"+beans[i].device+"</td>" +
				"<td>"+(beans[i].averagePrice*1).toFixed(2)+"</td>" +
				"<td>"+beans[i].clickNum+"</td>" +
				"<td>"+beans[i].adPay+"</td>" +
				"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
				"<td>"+beans[i].adBudget+"</td>" +
				"<td>"+((page-1)*5+(i+1))+"</td>"+
				"</tr>"
				
				$('#AdName').append(tbody);
			}
			var allBean=data.allBean;
			for (var i = 0; i < allBean.length;i++) {
				MyPays+=allBean[i].adPay*1;
				var proportion=(allBean[i].proportion*100).toFixed(2);
				proportions+=proportion*1;
				adBudgets+=allBean[i].adBudget*1;
				clickNums+=allBean[i].clickNum*1;
			}
			var tbodyTwo="<tr>" +
			"<td>总计</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>"+clickNums+"</td>" +
			"<td>"+MyPays.toFixed(2)+"</td>" +
		    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
		    "<td>"+adBudgets.toFixed(2)+"</td>" +
		    "<td>---</td>" + 
		 "</tr>";
			if(data.pages==data.pageNum){
				$('#AdName').append(tbodyTwo);
			}
				
		},
        error: function(){}        
	});
}

//为了分页所准备的事件
function ByAdNameTwo(page){
	var object = GetRequest();
	var endTime = $("#endTime").val(),startTime=$("#startTime").val();
	var adName=$("#adName").val();
	$("#endTime").val(endTime);
	$("#startTime").val(startTime);
	$("#adName").val(adName);
	if(endTime&&startTime){

    	$.ajax({
    		url : '/Lvscore_Advertiser_Maven/byAdname',
            data:{"startTime":startTime,"endTime":endTime,"adName":adName,"page":page},
            dataType:'json',
    		type : 'post',
    		success : function(data) {
    			$('#AdName').find("tr").eq(0).siblings("tr").remove();
    			var MyPays=0;	
				var beans = data.beans;
				var proportions=0;
				var recharges=0;
				var adBudgets=0;
				var clickNums=0;
    			//循环输出表格
    			for(var i = 0; i < beans.length;i++){
    				if(beans[i].device=="pc"){
						beans[i].device="PC";
					}else if(beans[i].device=="mobile"){
						beans[i].device="手机";
					}
    				if(beans[i].adForm=="video"){
						beans[i].adForm="视频";
					}else if(beans[i].adForm=="picture"){
						beans[i].adForm="图片";
					}
    				var tbody = "<tr><td>"+(i+1)+"</td>" +
    				"<td>"+beans[i].time +"</td>" +
    				"<td>"+beans[i].adName +"</td>" +
    				"<td>"+beans[i].adNum+"</td>" +
    				"<td>"+beans[i].industry+"</td>" +
    				"<td>"+beans[i].adForm+"</td>" +
    				"<td>"+beans[i].device+"</td>" +
    				"<td>"+(beans[i].averagePrice*1).toFixed(2)+"</td>" +
					"<td>"+beans[i].clickNum+"</td>" +
					"<td>"+beans[i].adPay+"</td>" +
    				"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
    				"<td>"+beans[i].adBudget+"</td>" +
    				"<td>"+((page-1)*5+(i+1))+"</td>"+
    				"</tr>"
    				
    				$('#AdName').append(tbody);
    			}
    			var allBean=data.allBean;
				for (var i = 0; i < allBean.length;i++) {
					MyPays+=allBean[i].adPay*1;
					var proportion=(allBean[i].proportion*100).toFixed(2);
					proportions+=proportion*1;
					adBudgets+=allBean[i].adBudget*1;
					clickNums+=allBean[i].clickNum*1;
				}
    			var tbodyTwo="<tr>" +
    			"<td>总计</td>" +
    			"<td>---</td>" +
    			"<td>---</td>" +
    			"<td>---</td>" +
    			"<td>---</td>" +
    			"<td>---</td>" +
    			"<td>---</td>" +
    			"<td>---</td>" +
    			"<td>"+clickNums+"</td>" +
				"<td>"+MyPays.toFixed(2)+"</td>" +
			    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
			    "<td>"+adBudgets.toFixed(2)+"</td>" +
    		    "<td>---</td>" + 
    		 "</tr>";
    			if(data.pages==data.pageNum){
    				$('#AdName').append(tbodyTwo);
				}
    				
    		},
            error: function(){}        
    	});
	}
}

